package com.example.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.example.entity.PointDefend;
import com.example.mapp.PointFlowMapp;
import com.example.util.SystemPointUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.example.entity.PromotionRecord;
import com.example.entity.RescueDrill;
import com.example.ext.entity.ExtRescueDrill;
import com.example.mapp.PromotionMapp;
import com.example.mapp.RescueDrillMapp;
import com.example.service.RescueDrillService;
import com.example.util.PromotionUtil;
import com.example.util.ResultUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

/**
 * Created by simon on 2017/11/20.
 */
@Service
public class RescueDrillServiceImpl implements RescueDrillService {
    private final static Logger logger = (Logger) LoggerFactory.getLogger(StatisticsServiceImpl.class);

    @Autowired
    private RescueDrillMapp rescueDrillMapp;

    @Autowired
    private PointFlowMapp pointFlowMapp;



    @Override
    public Map<String, Object> queryByIdList(ExtRescueDrill extRescueDrill) {
        List<ExtRescueDrill> list =null;
        Map<String,Object> map = new HashMap<String,Object>();
        try {
            Page<ExtRescueDrill> page = PageHelper.startPage(extRescueDrill.getCurrentPage(), extRescueDrill.getPageSize());
            list = rescueDrillMapp.findByIdList(extRescueDrill);
            map.put("list",list);
            map.put("total",page.getTotal());

        } catch (Exception e) {
            logger.error(e.getMessage());
            return ResultUtil.returnError("查询失败"+e.getMessage(),null);
        }
        return ResultUtil.returnSuccess("查询成功",map);
    }

    @Override
    @Transactional
    public Map<String, Object> add(RescueDrill rescueDrill) {
       try{
           int i = rescueDrillMapp.add(rescueDrill);
           // 添加积分救援演练
           if (rescueDrill != null) {
               // 获取维保单位缓存
               PointDefendInfoCache infoCache = new PointDefendInfoCache();
               PointDefend defendUnit = infoCache.getKeyByValue("rescue_drill_unit");

               // 获取维保单位ID
               long mid = Long.parseLong(rescueDrill.getMaintenancePartyId());
               Map<String, Object> maps = pointFlowMapp.findUserInfo(mid);
               long mark = (long) maps.get("mark");
               int round = (int) (mark + defendUnit.getMark());

               // 更新用户信息总积分
               String userIds = maps.get("userId").toString();
               int i1 = pointFlowMapp.updateMark(round, userIds);

               String userName = maps.get("userName").toString();
               String partyName = maps.get("partyName").toString();
               SystemPointUtil.insertFlow(pointFlowMapp, i1, defendUnit, round, userName, partyName, partyName+" 进行一次应急救援演练");


               // 获取使用单位ID
               long uid = rescueDrill.getDrillPartyId();
               Map<String, Object> mapUser = pointFlowMapp.findUserInfo(uid);
               long markUser = (long) mapUser.get("mark");
               int roundUser = (int) (markUser + defendUnit.getMark());

               // 更新用户信息总积分
               String userId = mapUser.get("userId").toString();
               int i2 = pointFlowMapp.updateMark(roundUser, userId);

               String name = mapUser.get("userName").toString();
               String pName = mapUser.get("partyName").toString();
               SystemPointUtil.insertFlow(pointFlowMapp, i2, defendUnit, roundUser, name, pName, pName+" 进行一次应急救援演练");

                // 获取安全管理员缓存
               PointDefend defendPerson = infoCache.getKeyByValue("rescue_drill_person");
               // 获取安全管理员Id
               Long personId = rescueDrill.getDrillRecordUserId();
               Map<String, Object> personMap = pointFlowMapp.findUserInfoByUserId(personId);
               long personMark = (long) personMap.get("mark");
               int personRound = (int) (personMark + defendPerson.getMark());

               // 更新用户信息总积分
               String personsId = personMap.get("userId").toString();
               int i3 = pointFlowMapp.updateMark(personRound, personsId);

               String personUserName = personMap.get("userName").toString();
               String personPartyName = personMap.get("partyName").toString();
               SystemPointUtil.insertFlow(pointFlowMapp, i3, defendPerson, personRound, personUserName, personPartyName, personUserName+" 进行一次应急救援演练");

           }

           if(i>0){
               return ResultUtil.returnSuccess("添加成功",null);
           }else{
               return ResultUtil.returnError("添加失败",null);
           }
       }catch (Exception e){
    	    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            logger.error("添加失败",e);
            return ResultUtil.returnError("添加失败",null);
       }
    }
}
